package com.lsyw.intergrate.controller;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lsyw.intergrate.common.Result;
import com.lsyw.intergrate.entity.MassifHole;
import com.lsyw.intergrate.entity.TransportTask;
import com.lsyw.intergrate.entity.common.PageInfo;
import com.lsyw.intergrate.service.TransportTaskService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 运输任务 前端控制器
 * </p>
 *
 * @author 刘磊
 * @since 2023-11-28
 */
@Api(tags = "运输任务")
@RestController
@RequestMapping("/transport-task")
public class TransportTaskController {


    @Autowired
    private TransportTaskService transportTaskService;


    /**
     * 通过id查询
     */
    @GetMapping("/get-by-id")
    @ApiOperation(value = "通过id查询", notes = "通过id查询", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "query", dataType = "String")
    })
    public Result getById(String id) {
        return Result.successResult(transportTaskService.getById(id));
    }

    /**
     * 新增
     */
    @PostMapping("/save")
    @ApiOperation(value = "新增", notes = "新增", httpMethod = "POST")
    public Result save(@RequestBody TransportTask transportTask) {
        transportTask.setStatus(0);
        transportTask.setDelFlag(0);
        transportTaskService.save(transportTask);
        return Result.successResult(null);
    }

    /**
     * 通过id删除
     */
    @GetMapping("/delete-by-id")
    @ApiOperation(value = "通过id删除", notes = "通过id删除", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "query", dataType = "String")
    })
    public Result delete(String id) {
        TransportTask transportTask = this.transportTaskService.getById(Long.parseLong(id));
        transportTask.setDelFlag(1);
        this.transportTaskService.updateById(transportTask);
        return Result.successResult(null);
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @ApiOperation(value = "修改", notes = "修改", httpMethod = "POST")
    public Result updateById(@RequestBody TransportTask transportTask) {
        transportTask.setDelFlag(0);
        transportTask.setGmtModified(new Date());
        transportTaskService.updateById(transportTask);
        return Result.successResult(null);
    }


    /**
     * 查询列表
     */
    @PostMapping("/list")
    @ApiOperation(value = "查询列表", notes = "查询列表", httpMethod = "POST")
    public Result list(@RequestBody TransportTaskReqVo transportTask) {
        final LambdaQueryWrapper<TransportTask> lambda = new QueryWrapper<TransportTask>().lambda();
        this.buildCondition(lambda, transportTask);
        return Result.successResult(transportTaskService.list(lambda));
    }

    /**
     * 分页查询
     */
    @PostMapping("/page")
    @ApiOperation(value = "分页查询", notes = "分页查询", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "condition:companyId", value = "运输公司/团队 ", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "condition:startTime", value = "开始时间  年-月-日", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "condition:endTime", value = "结束时间 年-月-日", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "condition:transportPlanId", value = "运输计划", required = false, paramType = "query", dataType = "String")
    })
    public Result page(@RequestBody PageInfo pageInfo) {
        Page<TransportTask> page = new Page<>();
        page.setSize(pageInfo.getPageSize());
        page.setCurrent(pageInfo.getPageNum());
        return Result.successResult(this.transportTaskService.pageList(page, pageInfo.getCondition()));
    }


    /**
     * 构造查询条件
     *
     * @param lambda
     * @param param
     */
    private void buildCondition(LambdaQueryWrapper<TransportTask> lambda, TransportTaskReqVo param) {
        if (!StringUtils.isEmpty(param.getId())) {
            lambda.eq(TransportTask::getId, param.getId());
        }
        if (!StringUtils.isEmpty(param.getTransportPlanId())) {
            lambda.eq(TransportTask::getTransportPlanId, param.getTransportPlanId());
        }
        if (!StringUtils.isEmpty(param.getWeight())) {
            lambda.eq(TransportTask::getWeight, param.getWeight());
        }
        if (!StringUtils.isEmpty(param.getCompanyId())) {
            lambda.eq(TransportTask::getCompanyId, param.getCompanyId());
        }
        if (!StringUtils.isEmpty(param.getRouteId())) {
            lambda.eq(TransportTask::getRouteId, param.getRouteId());
        }
        if (!StringUtils.isEmpty(param.getMassifId())) {
            lambda.eq(TransportTask::getMassifId, param.getMassifId());
        }
        if (!StringUtils.isEmpty(param.getGmtCreateUser())) {
            lambda.eq(TransportTask::getGmtCreateUser, param.getGmtCreateUser());
        }
        if (!CollectionUtils.isEmpty(param.getGmtStartList())) {
            lambda.ge(TransportTask::getGmtStart, param.getGmtStartList().get(0));
            lambda.le(TransportTask::getGmtStart, param.getGmtStartList().get(1));
        }
        if (!StringUtils.isEmpty(param.getGmtModifiedUser())) {
            lambda.eq(TransportTask::getGmtModifiedUser, param.getGmtModifiedUser());
        }
        if (!CollectionUtils.isEmpty(param.getGmtModifiedList())) {
            lambda.ge(TransportTask::getGmtModified, param.getGmtModifiedList().get(0));
            lambda.le(TransportTask::getGmtModified, param.getGmtModifiedList().get(1));
        }
        if (!StringUtils.isEmpty(param.getDelFlag())) {
            lambda.eq(TransportTask::getDelFlag, param.getDelFlag());
        }
        if (!CollectionUtils.isEmpty(param.getGmtCreateList())) {
            lambda.ge(TransportTask::getGmtCreate, param.getGmtCreateList().get(0));
            lambda.le(TransportTask::getGmtCreate, param.getGmtCreateList().get(1));
        }
        if (!CollectionUtils.isEmpty(param.getGmtEndList())) {
            lambda.ge(TransportTask::getGmtEnd, param.getGmtEndList().get(0));
            lambda.le(TransportTask::getGmtEnd, param.getGmtEndList().get(1));
        }
        lambda.orderBy(true, false, TransportTask::getId);
    }


    /**
     * 请求model
     */
    @Data
    @AllArgsConstructor
    private static class TransportTaskReqVo extends TransportTask {
        private List<String> gmtStartList; // 开始时间起止
        private List<String> gmtModifiedList; // 更新时间起止
        private List<String> gmtCreateList; // 创建时间起止
        private List<String> gmtEndList; // 结束时间起止
    }


}
